package com.forfunnet.minjian;

import android.app.Application;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.IBinder;


import com.applidium.shutterbug.utils.ShutterbugManager;
import com.forfunnet.minjian.rest.MyErrorHandler;
import com.forfunnet.minjian.rest.MyInterceptor;
import com.forfunnet.minjian.rest.MyResponseErrorHandler;
import com.forfunnet.minjian.rest.MyRestClient;
import com.forfunnet.minjian.rest.MyRestTemplate;
import com.forfunnet.minjian.ui.LoginActivity_;

import org.androidannotations.annotations.AfterInject;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.EApplication;
import org.androidannotations.annotations.sharedpreferences.Pref;
import org.androidannotations.rest.spring.annotations.RestService;
import org.springframework.http.HttpStatus;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;

/**
 * Created by RexQian on 2014/10/14.
 */
@EApplication
public class MyApplication extends Application implements MyResponseErrorHandler.ErrorListener {

    public String getVersion()
    {
        try {
            PackageInfo pInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
            return pInfo.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

    public int getVersionCode()
    {
        try {
            PackageInfo pInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
            return pInfo.versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public MyRestClient restClient()
    {
        return mRestClient;
    }

    public MyPrefs_ pref()
    {
        return mPrefs;
    }

//    public  UpdateService updateService()
//    {
//        return mUpdateService;
//    }

    @Pref
    MyPrefs_ mPrefs;

    @RestService
    MyRestClient mRestClient;

    @Bean
    MyErrorHandler mErrorHandler;

    @Bean
    MyInterceptor mInterceptor;

    @Bean
    MyRestTemplate mMyRestTemplate;

    MyResponseErrorHandler mErrHandler = new MyResponseErrorHandler();


    @AfterInject
    void afterInject()
    {
        String cookie = mPrefs.cookie().get();
        if(cookie != null && !cookie.isEmpty())
        {
            ShutterbugManager manager = ShutterbugManager.getSharedImageManager(this);
            manager.setRequestProperty("Cookie", "ss-id=" + cookie);
            mRestClient.setCookie("ss-id", cookie);
        }
        mMyRestTemplate.setMessageConverters(mRestClient.getRestTemplate().getMessageConverters());
        mRestClient.setRestTemplate(mMyRestTemplate);
        mRestClient.getRestTemplate().setRequestFactory(new HttpComponentsClientHttpRequestFactory());
        mRestClient.setRestErrorHandler(mErrorHandler);
        mRestClient.getRestTemplate().setErrorHandler(mErrHandler);
        mInterceptor.setRestClient(mRestClient);
        mRestClient.getRestTemplate().getInterceptors().add(mInterceptor);
        mErrHandler.setErrorListener(this);
        //bindService();
    }

    boolean showStartActivity = true;
    public boolean isShowStartActivity()
    {
        return showStartActivity;
    }

    public void setShowStartActivity(boolean isShow)
    {
        showStartActivity = isShow;
    }

    @Override
    public void onError(HttpStatus status) {
        if(status == HttpStatus.UNAUTHORIZED) {
            LoginActivity_.intent(this).flags(Intent.FLAG_ACTIVITY_NEW_TASK)
                    .start();
        }
    }

    int mActiveActivityCount = 0;
    long mEnterBackgroundTime = 0;
    long mShowBackScreenTime = 5*60*1000; //5 min

    public void onActivityStart()
    {
        mActiveActivityCount++;
        if(mActiveActivityCount == 1)
        {
            //enter foreground
            if(System.currentTimeMillis() - mEnterBackgroundTime > mShowBackScreenTime)
            {
                //show back screen
            }
        }
    }

    public void onActivityStop()
    {
        mActiveActivityCount--;
        if(mActiveActivityCount == 0)
        {
            //enter background
            mEnterBackgroundTime = System.currentTimeMillis();
        }
    }

//    private boolean mBinded = false;
//    private UpdateService mUpdateService;
//
//    void bindService()
//    {
//        if (mBinded)
//        {
//            return;
//        }
//        if (bindService(new Intent(this, UpdateService.class), mConnection, Context.BIND_AUTO_CREATE))
//        {
//            mBinded = true;
//        }
//    }
//
//    void unbindService()
//    {
//        if (mBinded)
//        {
//            if (mUpdateService != null)
//            {
//                mUpdateService = null;
//            }
//            unbindService(mConnection);
//            mBinded = false;
//        }
//    }
//
//    private ServiceConnection mConnection = new ServiceConnection()
//    {
//        public void onServiceConnected(ComponentName className, IBinder service)
//        {
//            mUpdateService = ((UpdateService.LocalBinder) service).getService();
//            mUpdateService.startCheckUpdate();
//        }
//
//        public void onServiceDisconnected(ComponentName className)
//        {
//            mUpdateService = null;
//            mBinded = false;
//            bindService();
//        }
//    };
}
